package com.whfc.fse.xxl.job;

import com.alibaba.fastjson.JSON;
import com.whfc.XxlJobConfig;
import com.whfc.common.constant.QueueConst;
import com.whfc.common.enums.DevicePlatform;
import com.whfc.common.third.ansafe.AnSafeApi;
import com.whfc.common.third.ansafe.entity.CraneWork;
import com.whfc.common.third.ansafe.entity.CraneWorkLoop;
import com.whfc.common.third.ansafe.entity.GantryWork;
import com.whfc.common.third.yuke.YukeApi;
import com.whfc.common.third.yuke.result.*;
import com.whfc.common.util.MathUtil;
import com.whfc.fse.dao.FseCraneMapper;
import com.whfc.fse.dao.FseGantryMapper;
import com.whfc.fse.dao.FseLiftMapper;
import com.whfc.fse.dto.*;
import com.whfc.fse.entity.FseLift;
import com.whfc.fse.enums.FseLiftDoorStatus;
import com.whfc.fse.enums.FseWarnRuleType;
import com.whfc.fse.manager.FseGantryMgr;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 特种设备数据同步 定时任务
 *
 * @author : qzexing
 * @version : 1.0
 * @date : 2020/7/14 14:38
 */
@Component
@ConditionalOnBean(XxlJobConfig.class)
public class FseDataSyncJob {

    private String yukeAccessToken = null;

    @Autowired
    private YukeApi yukeApi;

    @Autowired
    private AnSafeApi anSafeApi;

    @Autowired
    private FseLiftMapper fseLiftMapper;

    @Autowired
    private FseCraneMapper fseCraneMapper;

    @Autowired
    private FseGantryMapper fseGantryMapper;

    @Autowired
    private FseGantryMgr fseGantryMgr;

    @Resource(name = "baseRabbitTemplate")
    private AmqpTemplate amqpTemplate;

    /**
     * 同步宇科塔机基本信息
     */
    @XxlJob("syncYukeCraneInfo")
    public void syncYukeCraneInfo() {
        XxlJobHelper.log("开始同步塔吊设备基本信息, syncYukeCraneInfo");
        // 获取宇科塔机设备SN列表
        List<String> snList = fseCraneMapper.selectSnByPlatform(DevicePlatform.yuke.name());
        if (snList.isEmpty()) {
            return;
        }
        // 获取token
        getAccessToken();
        //获取塔吊信息
        YukeResult<List<CraneInfo>> yukeResult = yukeApi.getCraneInfo(yukeAccessToken, snList);
        //验证结果
        if (yukeResult == null || validateResultCode(yukeResult)) {
            XxlJobHelper.handleFail("获取塔吊结果验证失败，停止执行");
            return;
        }
        List<CraneInfo> craneInfoList = yukeResult.getData();
        if (craneInfoList == null || craneInfoList.isEmpty()) {
            XxlJobHelper.handleSuccess("没有需要处理的塔吊设备信息");
            return;
        }
        for (CraneInfo craneInfo : craneInfoList) {
            FseCraneParamDTO fseCraneParamDTO = new FseCraneParamDTO();
            fseCraneParamDTO.setPlatform(DevicePlatform.yuke.name());
            fseCraneParamDTO.setSn(craneInfo.getBlack_box_id());
            fseCraneParamDTO.setForeArmLength(craneInfo.getBoom_length());
            fseCraneParamDTO.setRearArmLength(craneInfo.getBalance_arm_length());
            fseCraneParamDTO.setTowerArmHeight(craneInfo.getBoom_height());
            fseCraneParamDTO.setMaxWeight(craneInfo.getMaximum_lifting_weight());
            fseCraneParamDTO.setMaxWindSpeed(craneInfo.getAlarm_wind_speed());
            fseCraneParamDTO.setMaxDipAngle(craneInfo.getAlarm_tilt());
            fseCraneParamDTO.setMaxMomentRatio(craneInfo.getMaximum_torque());
            // 发送队列处理
            amqpTemplate.convertAndSend(QueueConst.FSE_CRANE_PARAM_DATA, JSON.toJSONString(fseCraneParamDTO));
        }
    }

    /**
     * 同步宇科升降机基本信息
     */
    @XxlJob("syncYukeLiftInfo")
    public void syncYukeLiftInfo() {
        XxlJobHelper.log("开始同步升降机设备基本信息, syncYukeLiftInfo");
        // 获取宇科升降机设备SN列表
        List<String> snList = getLiftSnByPlatform(DevicePlatform.yuke.name());
        if (snList.isEmpty()) {
            return;
        }
        // 获取token
        getAccessToken();
        //获取升降机信息
        YukeResult<List<LiftInfo>> yukeResult = yukeApi.getLiftInfo(yukeAccessToken, snList);
        //验证结果
        if (yukeResult == null || validateResultCode(yukeResult)) {
            XxlJobHelper.handleFail("获取升降机结果验证失败，停止执行");
            return;
        }
        List<LiftInfo> liftInfoList = yukeResult.getData();
        if (liftInfoList == null || liftInfoList.isEmpty()) {
            XxlJobHelper.handleSuccess("没有需要处理的升降机设备信息");
            return;
        }
        for (LiftInfo liftInfo : liftInfoList) {
            FseLiftParamDTO fseLiftParamDTO = new FseLiftParamDTO();
            fseLiftParamDTO.setPlatform(DevicePlatform.yuke.name());
            fseLiftParamDTO.setSn(liftInfo.getHoist_box_id());
            fseLiftParamDTO.setMaxWeight(liftInfo.getMaximum_weight_alarm_t());
            fseLiftParamDTO.setMaxHeight(liftInfo.getMaximum_rise_height());
            fseLiftParamDTO.setMaxPersonNo(liftInfo.getMaximum_carrying_number());
            fseLiftParamDTO.setMaxDipAngle(liftInfo.getTilt_alarm());
            fseLiftParamDTO.setMaxSpeed(liftInfo.getMaximum_speed_alarm());
            // 发送队列处理
            amqpTemplate.convertAndSend(QueueConst.FSE_LIFT_PARAM_DATA, JSON.toJSONString(fseLiftParamDTO));
        }

    }

    /**
     * 同步宇科塔机实时数据
     */
    @XxlJob("syncYukeCraneRealData")
    public void syncYukeCraneRealData() {
        XxlJobHelper.log("开始同步宇科塔机实时数据");
        // 获取宇科塔机设备SN列表
        List<String> snList = fseCraneMapper.selectSnByPlatform(DevicePlatform.yuke.name());
        if (snList.isEmpty()) {
            return;
        }
        //塔机实时数据 每3分钟同步一次
        YukeResult<List<CraneRealData>> yukeResult = yukeApi.getCraneRealData(yukeAccessToken, snList, 3);
        //验证结果
        if (yukeResult == null || validateResultCode(yukeResult)) {
            XxlJobHelper.handleFail("获取升降机结果验证失败，停止执行");
            return;
        }
        List<CraneRealData> realDataList = yukeResult.getData();
        if (realDataList == null || realDataList.isEmpty()) {
            XxlJobHelper.handleSuccess("没有需要同步塔吊实时数据");
            return;
        }
        for (CraneRealData craneRealData : realDataList) {
            FseCraneDataDTO fseCraneDeviceData = new FseCraneDataDTO();
            fseCraneDeviceData.setPlatform(DevicePlatform.yuke.name());
            fseCraneDeviceData.setSn(craneRealData.getBlack_box_id());
            fseCraneDeviceData.setDipAngle(craneRealData.getCrane_tilt_angle());
            fseCraneDeviceData.setHeight(craneRealData.getCrane_height());
            fseCraneDeviceData.setRange(craneRealData.getCrane_range());
            fseCraneDeviceData.setTime(craneRealData.getCrane_time());
            fseCraneDeviceData.setTurnAngle(craneRealData.getCrane_rotation());
            fseCraneDeviceData.setWeight(craneRealData.getCrane_elevating_capacity());
            fseCraneDeviceData.setWindSpeed(craneRealData.getCrane_wind_speed());
            fseCraneDeviceData.setMomentRatio(craneRealData.getCrane_torque_percentage());
            //报警状态
            Integer craneAlarmReason = craneRealData.getCrane_alarm_reason();
            fseCraneDeviceData.setCraneStatus(parseYukeCraneValue(craneAlarmReason));
            // 发送队列处理
            amqpTemplate.convertAndSend(QueueConst.FSE_CRANE_REAL_DATA, JSON.toJSONString(fseCraneDeviceData));
        }
    }

    /**
     * 同步宇科升降机实时数据
     */
    @XxlJob("syncYukeLiftRealData")
    public void syncYukeLiftRealData() {
        XxlJobHelper.log("开始同步宇科升降机实时数据");
        // 获取宇科升降机设备SN列表
        List<String> snList = getLiftSnByPlatform(DevicePlatform.yuke.name());
        if (snList.isEmpty()) {
            return;
        }
        YukeResult<List<LiftRealData>> yukeResult = yukeApi.getLiftRealData(yukeAccessToken, snList, 3);
        //验证结果
        if (yukeResult == null || validateResultCode(yukeResult)) {
            XxlJobHelper.handleFail("获取升降机结果验证失败，停止执行");
            return;
        }
        List<LiftRealData> realDataList = yukeResult.getData();
        if (realDataList == null || realDataList.isEmpty()) {
            XxlJobHelper.handleSuccess("没有需要同步的升降机实时数据");
            return;
        }
        for (LiftRealData liftRealData : realDataList) {
            FseLiftDataDTO fseLiftData = new FseLiftDataDTO();
            fseLiftData.setPlatform(DevicePlatform.yuke.name());
            fseLiftData.setSn(liftRealData.getHoist_box_id());
            //保存升降机实时数据
            Integer frontDoorState = null;
            Integer backDoorState = null;
            String lockState = liftRealData.getLock_state();
            if (StringUtils.isNotEmpty(lockState)) {
                //从右到左 第0位前门,1开启0关闭  第1位后门,1开启0关闭
                String frontDoor = lockState.substring(lockState.length() - 1);
                if ("1".equals(frontDoor)) {
                    frontDoorState = FseLiftDoorStatus.OPEN.getValue();
                } else {
                    frontDoorState = FseLiftDoorStatus.CLOSE.getValue();
                }
                String backDoor = lockState.substring(lockState.length() - 2, lockState.length() - 1);
                if ("1".equals(backDoor)) {
                    backDoorState = FseLiftDoorStatus.OPEN.getValue();
                } else {
                    backDoorState = FseLiftDoorStatus.CLOSE.getValue();
                }
            }
            fseLiftData.setFloor(0);
            fseLiftData.setDipAngle(MathUtil.float2Double(liftRealData.getReal_time_gradient1()));
            fseLiftData.setHeight(MathUtil.float2Double(liftRealData.getReal_time_height()));
            fseLiftData.setPersonNo(liftRealData.getReal_time_number_of_people());
            fseLiftData.setSpeed(MathUtil.float2Double(liftRealData.getWind_speed()));
            fseLiftData.setWeight(MathUtil.float2Double(liftRealData.getReal_time_lifting_weight()));
            fseLiftData.setWindSpeed(MathUtil.float2Double(liftRealData.getWind_speed()));
            fseLiftData.setTime(liftRealData.getHoist_time());
            fseLiftData.setFrontDoorState(frontDoorState);
            fseLiftData.setBackDoorState(backDoorState);
            // 处理报警
            fseLiftData.setLiftStatus(parseYukeLiftValue(liftRealData.getHoist_alarm_reason()));
            // 推送数据
            amqpTemplate.convertAndSend(QueueConst.FSE_LIFT_REAL_DATA, JSON.toJSONString(fseLiftData));
        }
    }

    /**
     * 同步宇科塔机运行记录
     */
    @XxlJob("syncYukeCraneRecordData")
    public void syncYukeCraneRecordData() {
        XxlJobHelper.log("开始同步宇科塔机运行记录");
        // 获取宇科塔机设备SN列表
        List<String> snList = fseCraneMapper.selectSnByPlatform(DevicePlatform.yuke.name());
        if (snList.isEmpty()) {
            return;
        }
        Date now = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(now);
        //运行记录是十五分钟同步一次，那么此处就获取15分钟内的运行记录
        calendar.add(Calendar.MINUTE, -15);
        Date startTime = calendar.getTime();
        YukeResult<List<CraneRecordData>> yukeResult = yukeApi.getCraneRecordData(yukeAccessToken, snList, startTime, now);
        //验证结果
        if (yukeResult == null || validateResultCode(yukeResult)) {
            XxlJobHelper.handleFail("获取升降机结果验证失败，停止执行");
            return;
        }
        List<CraneRecordData> craneRecordDataList = yukeResult.getData();
        if (craneRecordDataList == null || craneRecordDataList.isEmpty()) {
            XxlJobHelper.handleSuccess("没有需要同步的塔机历史数据");
            return;
        }
        for (CraneRecordData craneRecordData : craneRecordDataList) {
            FseCraneRecordDTO craneRecordDTO = new FseCraneRecordDTO();
            craneRecordDTO.setPlatform(DevicePlatform.yuke.name());
            craneRecordDTO.setSn(craneRecordData.getBlack_box_id());
            craneRecordDTO.setStartTime(craneRecordData.getStart_time());
            craneRecordDTO.setEndTime(craneRecordData.getEnd_time());
            craneRecordDTO.setStartHeight(craneRecordData.getLifting_point_height());
            craneRecordDTO.setEndHeight(craneRecordData.getUnloading_point_height());
            craneRecordDTO.setStartRange(craneRecordData.getLifting_point_range());
            craneRecordDTO.setEndRange(craneRecordData.getUnloading_point_range());
            craneRecordDTO.setMaxWeight(craneRecordData.getCycle_maximum_weight());
            // 推送数据
            amqpTemplate.convertAndSend(QueueConst.FSE_CRANE_RECORD_DATA, JSON.toJSONString(craneRecordDTO));
        }
    }

    /**
     * 同步宇科升降机运行记录
     */
    @XxlJob("syncYukeLiftRecordData")
    public void syncYukeLiftRecordData() {
        XxlJobHelper.log("开始同步宇科升降机运行记录");
        // 获取宇科升降机设备SN列表
        List<String> snList = getLiftSnByPlatform(DevicePlatform.yuke.name());
        if (snList.isEmpty()) {
            return;
        }
        Date now = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(now);
        //运行记录是十五分钟同步一次，那么此处就获取15分钟内的运行记录
        calendar.add(Calendar.MINUTE, -15);
        Date startTime = calendar.getTime();
        YukeResult<List<LiftRecordData>> yukeResult = yukeApi.getLiftRecordData(yukeAccessToken, snList, startTime, now);
        //验证结果
        if (yukeResult == null || validateResultCode(yukeResult)) {
            XxlJobHelper.handleFail("获取升降机结果验证失败，停止执行");
            return;
        }
        List<LiftRecordData> liftRecordDataList = yukeResult.getData();
        if (liftRecordDataList == null || liftRecordDataList.isEmpty()) {
            XxlJobHelper.handleSuccess("没有需要处理的升降机数据");
            return;
        }
        for (LiftRecordData liftRecordData : liftRecordDataList) {
            FseLiftRecordDTO liftRecordDTO = new FseLiftRecordDTO();
            liftRecordDTO.setPlatform(DevicePlatform.yuke.name());
            liftRecordDTO.setSn(liftRecordData.getHoist_box_id());
            liftRecordDTO.setStartTime(liftRecordData.getWork_cycle_start_time());
            liftRecordDTO.setEndTime(liftRecordData.getWork_cycle_end_time());
            liftRecordDTO.setWeight(MathUtil.float2Double(liftRecordData.getThe_load()));
            liftRecordDTO.setPersonNo(liftRecordData.getThe_load_number());
            liftRecordDTO.setStartHeight(MathUtil.float2Double(liftRecordData.getThe_rise_start_height()));
            liftRecordDTO.setEndHeight(MathUtil.float2Double(liftRecordData.getThe_rise_end_height()));
            liftRecordDTO.setStrokeHeight(MathUtil.float2Double(liftRecordData.getThe_rise_stroke_height()));
            liftRecordDTO.setAvgSpeed(MathUtil.float2Double(liftRecordData.getThe_rise_average_speed()));
            liftRecordDTO.setDirection(liftRecordData.getThe_rise_direction());
            liftRecordDTO.setDipAngleX(MathUtil.float2Double(liftRecordData.getThe_rise_maximum_x_direction_gradient()));
            liftRecordDTO.setDipAngleY(MathUtil.float2Double(liftRecordData.getThe_rise_maximum_y_direction_gradient()));
            // 推送数据
            amqpTemplate.convertAndSend(QueueConst.FSE_LIFT_RECORD_DATA, JSON.toJSONString(liftRecordDTO));
        }
    }

    /**
     * 同步安拾塔机实时数据
     */
    @XxlJob("syncAnSafeCraneRealData")
    public void syncAnSafeCraneRealData() {
        XxlJobHelper.log("开始同步安拾塔机实时数据");
        // 获取安拾塔机设备SN列表
        List<String> snList = fseCraneMapper.selectSnByPlatform(DevicePlatform.ansafe.name());
        if (snList.isEmpty()) {
            return;
        }
        // 获取数据
        for (String sn : snList) {
            try {
                CraneWork craneWork = anSafeApi.getCraneWork(sn);
                if (craneWork == null) {
                    continue;
                }
                FseCraneDataDTO fseCraneDeviceData = new FseCraneDataDTO();
                fseCraneDeviceData.setPlatform(DevicePlatform.ansafe.name());
                fseCraneDeviceData.setSn(craneWork.getSn());
                fseCraneDeviceData.setDipAngle(craneWork.getRtDipAngle());
                fseCraneDeviceData.setHeight(craneWork.getRtHeight());
                fseCraneDeviceData.setRange(craneWork.getRtLength());
                fseCraneDeviceData.setTime(craneWork.getDataTime());
                fseCraneDeviceData.setTurnAngle(craneWork.getRtRotate());
                fseCraneDeviceData.setWeight(craneWork.getRtWeight());
                fseCraneDeviceData.setWindSpeed(craneWork.getRtWindSPD());
                //报警状态
                fseCraneDeviceData.setCraneStatus(parseAnSafeCraneValue(craneWork));
                // 发送队列处理
                amqpTemplate.convertAndSend(QueueConst.FSE_CRANE_REAL_DATA, JSON.toJSONString(fseCraneDeviceData));
            } catch (Exception e) {
                XxlJobHelper.handleFail("同步安拾塔机实时数据是失败. sn:" + sn);
            }
        }
    }

    /**
     * 同步安拾塔机运行记录
     */
    @XxlJob("syncAnSafeCraneRecordData")
    public void syncAnSafeCraneRecordData() {
        XxlJobHelper.log("开始同步安拾塔机运行记录");
        // 获取宇科塔机设备SN列表
        List<String> snList = fseCraneMapper.selectSnByPlatform(DevicePlatform.ansafe.name());
        if (snList.isEmpty()) {
            return;
        }
        for (String sn : snList) {
            CraneWorkLoop craneWorkLoop = anSafeApi.getCraneWorkLoop(sn);
            if (craneWorkLoop == null) {
                continue;
            }
            FseCraneRecordDTO craneRecordDTO = new FseCraneRecordDTO();
            craneRecordDTO.setPlatform(DevicePlatform.ansafe.name());
            craneRecordDTO.setSn(craneWorkLoop.getSn());
            craneRecordDTO.setTime(craneWorkLoop.getTime());
            craneRecordDTO.setStartTime(craneWorkLoop.getLift_Time());
            craneRecordDTO.setEndTime(craneWorkLoop.getUnload_Time());
            craneRecordDTO.setStartHeight(craneWorkLoop.getLift_Height());
            craneRecordDTO.setEndHeight(craneWorkLoop.getUnload_Height());
            craneRecordDTO.setStartRange(craneWorkLoop.getLift_Range());
            craneRecordDTO.setEndRange(craneWorkLoop.getUnload_Range());
            craneRecordDTO.setMaxWeight(craneWorkLoop.getMax_Weight());
            // 推送数据
            amqpTemplate.convertAndSend(QueueConst.FSE_CRANE_RECORD_DATA, JSON.toJSONString(craneRecordDTO));
        }
    }


    /**
     * 同步安拾龙门吊数据
     */
    @XxlJob("syncAnSafeGantryRealData")
    public void syncAnSafeGantryRealData() {
        XxlJobHelper.log("安拾龙门吊实时数据同步");
        // 获取安拾平台龙门吊设备SN
        List<String> snList = fseGantryMapper.selectSnByPlatform(DevicePlatform.ansafe.name());
        if (snList.isEmpty()) {
            return;
        }
        for (String sn : snList) {
            try {
                GantryWork gantryWork = anSafeApi.getGantryWork(sn);
                if (gantryWork == null) {
                    return;
                }
                FseGantryDataDTO gantryDataDTO = new FseGantryDataDTO();
                //数据转换
                gantryDataDTO.setPlatform(DevicePlatform.ansafe.name());
                gantryDataDTO.setSn(gantryWork.getSn());
                gantryDataDTO.setTime(gantryWork.getTime());
                gantryDataDTO.setHeight(gantryWork.getMainhockheight());
                gantryDataDTO.setShift(gantryWork.getDecline());
                gantryDataDTO.setWindSpeed(gantryWork.getWindspeed());
                gantryDataDTO.setMainHookLiftHeight(gantryWork.getMainhockheight());
                gantryDataDTO.setSubHookLiftHeight(gantryWork.getVicehockheight());
                gantryDataDTO.setMainHookLiftWeight(gantryWork.getMainhockweight());
                gantryDataDTO.setSubHookLiftWeight(gantryWork.getVicehockweight());
                //主钩超载报警
                int mainHookWarn = 0;
                if (1 == gantryWork.getMainoverloadwarn()) {
                    mainHookWarn = 1;
                }
                gantryDataDTO.setMainHookWarn(mainHookWarn);
                //副钩超载报警
                int subHookWarn = 0;
                if (1 == gantryWork.getViceoverloadwarn()) {
                    subHookWarn = 1;
                }
                gantryDataDTO.setSubHookWarn(subHookWarn);
                fseGantryMgr.addData(gantryDataDTO);
            } catch (Exception e) {
                XxlJobHelper.log("安拾龙门吊实时数据同步失败,sn:{}, error:{}", sn, e);
            }
        }
    }

    /**
     * 获取宇科accessToken
     */
    private void getAccessToken() {
        if (StringUtils.isEmpty(yukeAccessToken)) {
            yukeAccessToken = yukeApi.getAccessToken();
        }
    }

    /**
     * 验证宇科返回码
     *
     * @param result 请求响应
     * @return 是否验证成功
     */
    private boolean validateResultCode(YukeResult<?> result) {
        String code = result.getCode();
        if (YukeApi.TOKEN_EXPIRED_CODE.equals(code)) {
            //token过期
            yukeAccessToken = yukeApi.getAccessToken();
            if (StringUtils.isEmpty(yukeAccessToken)) {
                XxlJobHelper.log("获取token为空,停止后续执行...");
                return true;
            }
        }
        return false;
    }

    /**
     * 获取宇科升降机设备SN列表
     *
     * @param platform 平台
     * @return 升降机设备SN
     */
    private List<String> getLiftSnByPlatform(String platform) {
        List<FseLift> fseLifts = fseLiftMapper.selectLiftListByPlatform(platform);
        List<String> snList = new ArrayList<>();
        for (FseLift fseLift : fseLifts) {
            if (StringUtils.isNotBlank(fseLift.getLeftSn())) {
                snList.add(fseLift.getLeftSn());
            }
            if (StringUtils.isNotBlank(fseLift.getRightSn())) {
                snList.add(fseLift.getRightSn());
            }
        }
        return snList;
    }

    /**
     * 处理宇科塔机报警
     *
     * @param code 宇科报警编码
     * @return 风潮报警编码
     */
    private Integer parseYukeCraneValue(Integer code) {
        if (code == null) {
            return 0;
        }
        switch (code) {
            case 111:
            case 112:
            case 113:
            case 114:
                return FseWarnRuleType.CRANE_ANTI_COLLISION.value();
            case 12:
                return FseWarnRuleType.CRANE_WEIGHT.value();
            case 13:
                return FseWarnRuleType.CRANE_FORCE_MOMENT.value();
            case 14:
                return FseWarnRuleType.CRANE_TURN.value();
            case 15:
                return FseWarnRuleType.CRANE_WIND_SPEED.value();
            case 101:
                return FseWarnRuleType.CRANE_RANGE.value();
            case 102:
                return FseWarnRuleType.CRANE_HEIGHT_RANGE.value();
            case 103:
                return FseWarnRuleType.CRANE_ROTARY_LIMIT.value();
            default:
                return 0;
        }
    }

    /**
     * 处理宇科升降机报警
     *
     * @param code 宇科报警编码
     * @return 风潮报警编码
     */
    private Integer parseYukeLiftValue(Integer code) {
        if (code == null) {
            return 0;
        }
        switch (code) {
            case 1:
            case 4:
                return FseWarnRuleType.LIFT_WEIGH.value();
            case 2:
            case 3:
                return FseWarnRuleType.LIFT_SPEED.value();
            default:
                return 0;
        }
    }

    /**
     * 处理安拾塔机报警
     *
     * @param craneWork 安拾数据
     * @return 风潮报警编码
     */
    private Integer parseAnSafeCraneValue(CraneWork craneWork) {
        int anSafeWarnNum = 2;
        if (craneWork == null || anSafeWarnNum != craneWork.getIsAlarm()) {
            return 0;
        }
        if (anSafeWarnNum == craneWork.getAl_ObstacleCollision()
                || anSafeWarnNum == craneWork.getAl_TowerCollision()) {
            return FseWarnRuleType.CRANE_ANTI_COLLISION.value();
        } else if (anSafeWarnNum == craneWork.getAl_Load()) {
            return FseWarnRuleType.CRANE_WEIGHT.value();
        } else if (anSafeWarnNum == craneWork.getAl_TopLimit()) {
            return FseWarnRuleType.CRANE_HEIGHT_RANGE.value();
        } else if (anSafeWarnNum == craneWork.getAl_LeftLimit()) {
            return FseWarnRuleType.CRANE_ROTARY_LIMIT.value();
        } else if (anSafeWarnNum == craneWork.getAl_Windspeed()) {
            return FseWarnRuleType.CRANE_WIND_SPEED.value();
        } else if (anSafeWarnNum == craneWork.getAl_Incline()) {
            return FseWarnRuleType.CRANE_TURN.value();
        }
        return 0;
    }

}
